package mt.client;

import java.util.LinkedList;


import javax.swing.table.AbstractTableModel;


import mt.Order;

/**
 * This Model builds the structure of the tables in GraphicInterface.
 * @author tom93d
 *
 */
@SuppressWarnings("serial")
public class JTableModel  extends AbstractTableModel {

	public String[] columnNames = {"Nickname","Stock","Number of Units","PricePerUnit"};

	@SuppressWarnings("rawtypes")
	public Class[] m_colTypes = { String.class,String.class, Integer.class, Double.class };

	private LinkedList<Order> data = new LinkedList<Order>();

	private TableType type;

	public JTableModel(LinkedList<Order> data) {
		this.data=data;
	}

	@Override
	public int getColumnCount() {

		return columnNames.length;
	}


	@Override
	public int getRowCount() {

		return data.size();
	}
	public String getColumnName(int col) {
		return columnNames[col];
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int col) {
		return m_colTypes[col];
	}


	@Override
	public Object getValueAt(int row, int col) {
		Order o = (Order) (data.get(row));
		switch (col) {
		case 0:
			//System.out.println("Case 0: "+data.get(row).getNickname() );
			return o.getNickname();	
			//return data.get(row).getNickname();
		case 1:
		//	System.out.println("Case 1 : "+data.get(row).getStock() );
			//	return data.get(row).getStock();
			return o.getStock();
		case 2:
			//System.out.println("Case 2: "+String.valueOf(data.get(row).getNumberOfUnits()));
			//return String.valueOf(data.get(row).getNumberOfUnits());
			return o.getNumberOfUnits();
		case 3:
		//	System.out.println("Case 3: "+data.get(row).getPricePerUnit());
			//return ""+data.get(row).getPricePerUnit();
			return o.getPricePerUnit();



		}
		return new String();
	}


	public void addEntry(Order order){
		data.add(order);
		fireTableDataChanged();
	}

public void updateJTable(){
	fireTableDataChanged();
}

	public LinkedList<Order> getData() {
		return this.data;
	}

	enum TableType{
		SELLING_TABLE,
		HISTORY_TABLE,
		PURCHASING_TABLE
	}

	public void setType(TableType type) {
		this.type = type;
	}

	public TableType getType() {
		return this.type;
	}
}